package com.example.intelligenttransportationteam1;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;

import com.example.intelligenttransportationteam1.fragment.ETCFragment;
import com.example.intelligenttransportationteam1.fragment.EnviFragment;
import com.example.intelligenttransportationteam1.fragment.IndexFragment;
import com.example.intelligenttransportationteam1.fragment.LightFragment;
import com.google.android.material.navigation.NavigationView;

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private FrameLayout framelayout;
    private NavigationView navigation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        setSupportActionBar(toolbar);//使用ToolBar替换系统自带的ActionBar
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左上角显示返回建
        getSupportActionBar().setHomeButtonEnabled(true);//设置返回键可用

//       将工具栏和侧滑菜单绑定
//        创建切换对象toggle
        ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.app_name,R.string.app_name){
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };
        toggle.syncState();//设置为同步
        drawerLayout.addDrawerListener(toggle);

//        初始化Fragment
        getSupportFragmentManager().beginTransaction()
                .add(R.id.framelayout,IndexFragment.newInstance("",""),"")
                .commit();
        toolbar.setTitle("智能交通系统");

//        给侧滑菜单设置选择事件，在处理单元中，根据选项显示Fragment
        navigation.setNavigationItemSelectedListener(item -> {
            item.setCheckable(true);//当前选项
            switch (item.getItemId()){
                case R.id.itemIndex:
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.framelayout,IndexFragment.newInstance("",""),"")
                            .commit();
                    toolbar.setTitle("智能交通系统");
                    break;
                case R.id.itemETC:
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.framelayout, ETCFragment.newInstance("",""),"")
                            .commit();
                    toolbar.setTitle("ETC账户");
                    break;
                case R.id.itemEnvi:
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.framelayout, EnviFragment.newInstance("",""),"")
                            .commit();
                    toolbar.setTitle("环境指标");
                    break;
                case R.id.itemLight:
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.framelayout, LightFragment.newInstance("",""),"")
                            .commit();
                    toolbar.setTitle("交通灯");
                    break;
                case R.id.itemExit:
                    MainActivity.this.finish();
                    break;
            }
            drawerLayout.closeDrawers();//关闭侧栏
            return true;
        });

    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        framelayout = (FrameLayout) findViewById(R.id.framelayout);
        navigation = (NavigationView) findViewById(R.id.navigation);
    }
}